// pages/product/groupList.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;

let app = getApp()

var start = 0;

Page(Object.assign({}, Zan.Tab, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    groupList: {},
    nodata: {
      msg: '暂无拼团商品',
      notype: 1,
      imgurl: '../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    network._post_json(this, api.groupList, {}, success);
    //this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.groupList, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  onProductItemClick: function(e){
    var isGroups = e.currentTarget.dataset.kaituandata.isGroups;
    var spellGroupsId = e.currentTarget.dataset.kaituandata.id;
    if (e.currentTarget.dataset.kaituandata.spellGroupsDetail){
      var userSpellGroupId = e.currentTarget.dataset.kaituandata.spellGroupsDetail.id;
      var endtime = e.currentTarget.dataset.kaituandata.spellGroupsDetail.endDate;
      var originator = e.currentTarget.dataset.kaituandata.spellGroupsDetail.originator;
      var order = e.currentTarget.dataset.kaituandata.spellGroupsDetail.orderProductDetail;
      var Cantuan_spec = {};
      var Cantuan_specs = [];
      if (order.productSpec && !order.productSpec1 && !order.productSpec2) {
        var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec
      } else if (order.productSpec1 && !order.productSpec2) {
        var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.kaituandata.specType2 + ':' + order.productSpec1
      } else if (order.productSpec2) {
        var Spec = e.currentTarget.dataset.kaituandata.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.kaituandata.specType2 + ':' + order.productSpec1 + ',' + e.currentTarget.dataset.kaituandata.specType3 + ':' + order.productSpec2
      }
      var count = e.currentTarget.dataset.kaituandata.spellGroupsDetail.orderProductDetail.count;
      Cantuan_spec.Spec = Spec;
      Cantuan_spec.count = count
      Cantuan_specs.push(Cantuan_spec);
      try {
        wx.setStorageSync('Cantuan_spec', Cantuan_specs)
      } catch (e) {
      }
    }
    if (e.currentTarget.dataset.kaituandata.spellGroupsCombs){
      var sgCombId = e.currentTarget.dataset.kaituandata.spellGroupsCombs[0].id;
    }
    
    if (isGroups==2){
      //此商品我正在开团
      wx.navigateTo({
        url: '/pages/myself/myspellgroup/MySpellGroupDetails?userSpellGroupId=' + userSpellGroupId + '&spellGroupsId=' + spellGroupsId + '&sgCombId=' + sgCombId + '&type=1' + '&endtime=' + endtime + '&originator=' + originator
      })
    } else if (isGroups == 1){
      //此商品我正在参团
      wx.navigateTo({
        url: '/pages/myself/myspellgroup/MySpellGroupDetails?userSpellGroupId=' + userSpellGroupId + '&spellGroupsId=' + spellGroupsId + '&sgCombId=' + sgCombId + '&type=0' + '&endtime=' + endtime + '&originator=' + originator,
      })
    }else{
      //此商品没有开团或参团
      wx.navigateTo({
        url: '/pages/product/SpellGroupDetails?spellGroupId=' + spellGroupsId
      })
    }
    
    // if (spellGroupId && productid){
    //   wx.navigateTo({
    //     url: '/pages/product/SpellGroupDetails?spellGroupId=' + spellGroupId + '&productId=' + productid
    //   })
    // }
  }
}))

var success = function(that,res){

  if(res.data.data){
    for(var i=0;i<res.data.data.length;i++){
      res.data.data[i].proPrice = utils.toDecimal2(res.data.data[i].proPrice/100);
      res.data.data[i].sgPrice = utils.toDecimal2(res.data.data[i].sgPrice/100);
      if (res.data.data[i].spellGroupsCombs) {
          res.data.data[i].spellGroupsCombs[0].specValue1 = utils.toDecimal2(res.data.data[i].spellGroupsCombs[0].specValue1 / 100);
          res.data.data[i].spellGroupsCombs[0].specValue2 = utils.toDecimal2(res.data.data[i].spellGroupsCombs[0].specValue2 / 100);
       
      }
      if (res.data.data[i].spellGroupsComb) {
          res.data.data[i].spellGroupsComb.specValue1 = utils.toDecimal2(res.data.data[i].spellGroupsComb.specValue1 / 100);
          res.data.data[i].spellGroupsComb.specValue2 = utils.toDecimal2(res.data.data[i].spellGroupsComb.specValue2 / 100);
       
      }
    }
    
    
    that.setData({
      groupList: res.data.data
    })
  }
  
}

